package com.fz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.Orders;
import com.fz.service.OrdersService;
import com.fz.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.math.BigDecimal;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fz
 * @since 2024-08-20
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Resource
    OrdersService ordersService;

    @GetMapping("/list")
    ResultVo<IPage<Orders>> list(Long userId, Integer pageNo, Integer pageSize) {
        return ResultVo.success(ordersService.getList(userId, pageNo, pageSize));
    }

    @GetMapping("/searchByOrderId")
    ResultVo<IPage<Orders>> searchByOrderId(Long userId, Integer pageNo, Integer pageSize, String searchVal) {
        Page<Orders> ordersPage = ordersService.searchByOrderId(userId, pageNo, pageSize, searchVal);
        if (ordersPage.getTotal() == 0) {
            return ResultVo.failed("未查询到该订单");
        } else {
            return ResultVo.success(ordersPage, "查询成功");
        }
    }

    @PostMapping("/payTheOrder")
    ResultVo<Boolean> payTheOrder(String password, Long orderId, Long userId) {
        if (ordersService.payTheOrder(password, orderId, userId)) {
            return ResultVo.success(true, "支付成功！");
        } else {
            return ResultVo.failed("支付密码不正确，请重新输入");
        }
    }

    @GetMapping("/getById")
    ResultVo<Orders> getById(Long orderId) {
        return ResultVo.success(ordersService.getById(orderId));
    }

    @GetMapping("/getByOrderStatus")
    ResultVo<IPage<Orders>> getByOrderStatus(Long userId, String status, Integer pageNo, Integer pageSize) {
        return ResultVo.success(ordersService.getByOrderStatus(userId, status, pageNo, pageSize));
    }

    @PostMapping("/createOrder")
    ResultVo<Long> add(Long userId, BigDecimal totalAmount, Long[] goodsIds, Integer[] counts, Long storeId) {
        System.out.println(Arrays.toString(goodsIds));
        System.out.println(Arrays.toString(counts));
        return ResultVo.success(ordersService.createOrder(userId, totalAmount, goodsIds, counts, storeId), "创建订单成功");

    }

    @GetMapping("getByTimeRange")
    ResultVo<IPage<Orders>> getByTimeRange(Long userId, String maxTime, String minTime, Integer pageNo, Integer pageSize) {
        Page<Orders> ordersPage = ordersService.getByTimeRange(userId, maxTime, minTime, pageNo, pageSize);
        if(ordersPage.getTotal() == 0) {
            return ResultVo.failed("未查询到该时间段的订单");
        }else{
            return ResultVo.success(ordersPage,"查询成功");
        }
    }

    @PostMapping("/add")
    ResultVo<Boolean> add(Orders orders) {
        return ResultVo.success(ordersService.save(orders));

    }

    @PostMapping("/del")
    ResultVo<Boolean> del(Long id) {
        return ResultVo.success(ordersService.removeById(id), "删除订单成功");
    }

    @PostMapping("/updateStatus")
    ResultVo<Boolean> updateStatus(Long id, String orderStatus) {
        return ResultVo.success(ordersService.updateById(new Orders(id, orderStatus)));
    }
    @PostMapping("/batchShipOrders")
    ResultVo<Boolean> batchShipOrders(Long[] ids,String orderStatus){
        return ResultVo.success(ordersService.batchShipOrders(ids,orderStatus));
    }

}
